package com.glory.brepda.activity.mfg;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.glory.brepda.R;

import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;

import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.entity.wbScheduleEntity;
import com.glory.brepda.utils.NetUtils;
import com.glory.brepda.utils.ShowDialog;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;


import butterknife.BindView;
import butterknife.OnClick;

public class WbLingGuanActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.teNextLotId)
    TextView teNextLotId;
    @BindView(R.id.tedbLocation)
    TextView tedbLocation;
    @BindView(R.id.tewbLocation)
    TextView tewbLocation;
    @BindView(R.id.etCheckLotId)
    FreeUI_ClearEditText etCheckLotId;




    @Override
    protected int getLayoutResId() {
        return R.layout.activity_wb_lingguan;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("校验");

    }

    private void checkLot(String scanLotId) {
        if(StringUtils.isEmpty(scanLotId)){
            showToast("不能为空", false);
        }
        if(scanLotId.equals(teNextLotId.getText())){
//            AlertDialog dialog=null;
//            dialog = new AlertDialog.Builder(this).setTitle("压焊领管校验")
//                    .setMessage("批次正确，可以领管")
//                    .setIcon(R.drawable.lanjian)
//                    .setPositiveButton("确定",new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            // 在这里处理确定按钮的点击事件
//                            changeScheduleStete();
//                        }
//                    })
//                    .setNegativeButton("取消",null)
//                    .create();
//            dialog.show();
            ShowDialog.showDialog(WbLingGuanActivity.this,"批次正确，可以领管","压焊领管校验");
        }
        if(!scanLotId.equals(teNextLotId.getText())){
            ShowDialog.showDialog(WbLingGuanActivity.this,"批次有误，请核对信息","压焊领管校验");
        }
    }

    @Override
    protected void initData() {
        initEdit(etEqpId, this::getNextLot);
        initEdit(etCheckLotId, this::checkLot);
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (getText(etCheckLotId).equals("") ) {
                    showToast("批次号不能为空", false);
                    return;
                }
                checkLot(getText(etCheckLotId));
                break;
        }
    }

    //领管后改变排产记录状态
    private void changeScheduleStete(){
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "wbSchedule");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENTID",getText(etEqpId));
        map.addProperty("LOTID",teNextLotId.getText().toString());
        map.addProperty("ACTIONTYPE","CHANGESTATE");
        NetUtils.requestPostNet("IF.WBSCHEDULE","Common", WbLingGuanActivity.this,map,callBack);
    }

    private void getNextLot(String eqpID) {
        FreeUI_EntityCallBack<BaseResponseBean<wbScheduleEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<wbScheduleEntity>>
                (new TypeToken<BaseResponseBean<wbScheduleEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<wbScheduleEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(WbLingGuanActivity.this,false,getSupportFragmentManager())){
                    String lotid = response.body().getResponse().getBody().getLOTID();
                    String dbLocation = response.body().getResponse().getBody().getDbLocation();
                    String wbLocation = response.body().getResponse().getBody().getWbLocation();
                    teNextLotId.setText(StringUtils.isEmpty(lotid)?"无排产信息":lotid);
                    tedbLocation.setText(StringUtils.isEmpty(dbLocation)?"无排产信息":dbLocation);
                    tewbLocation.setText(StringUtils.isEmpty(wbLocation)?"无排产信息":wbLocation);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<wbScheduleEntity>> response) {
                super.onError(response);
                FreeApi_LogUtils.saveErrorMessages(response.getException(), "wbSchedule");
                loadError(response.getException(), "wbSchedule");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENTID",eqpID);
        map.addProperty("ACTIONTYPE","NEXTLOT");
        NetUtils.requestPostNet("IF.WBSCHEDULE","Common", WbLingGuanActivity.this,map,callBack);
    }


}
